/*
 * BuscaServlet.java
 *
 * Created on 16 de Junho de 2007, 15:58
 */

package action;

import java.io.*;
import java.net.*;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.*;
import javax.servlet.http.*;
import model.persistence.dao.FilmeDAO;
import model.persistence.dao.HibernateDAO;
import model.persistence.vo.Bairro;
import model.persistence.vo.Filme;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author $Leandro$
 * @version
 */
public class BuscaFilmesServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request,response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String strIdCinema = (String) request.getParameter("idCinema");        
                                
        DataOutputStream out = new DataOutputStream(response.getOutputStream());
        
        int idCinema = Integer.parseInt(strIdCinema);
               
        FilmeDAO fDao = new FilmeDAO();
        Collection result = fDao.getFilmesByCinema(idCinema);
        
        Iterator it = result.iterator();
        int count = result.size();
        out.writeInt(count);
        
        while(it.hasNext()) {
            Filme f = (Filme) it.next();
            try {
                out.write(f.serialize());
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
    }
}